package com.yonyou.dao;

import com.yonyou.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author wangchu
 * @date 2025-08-10 11:05
 */
@Slf4j
@Repository
public class LoginDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public LoginUser login(LoginUser user) {
        String sql = "SELECT g.* \n" +
                "FROM GL_CZY g\n" +
                "\tINNER JOIN OER_SEC_CZZF_LOGIN_WHITELIST w ON ( g.id = w.id OR g.czyCode = w.czyCode OR g.name = w.name ) \n" +
                "WHERE\n" +
                "\t( CONVERT ( VARCHAR, g.id ) = ? OR g.czyCode = ? OR g.name = ? ) \n" +
                "\tAND w.state = 1;";
        LoginUser loginUser = null;
        try {
            loginUser = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(LoginUser.class), user.getCzyCode(), user.getCzyCode(), user.getCzyCode());
        } catch (DataAccessException ex) {
            log.error("登录出现异常！", ex);
        }
        return loginUser;
    }
}